####################################################################################
###
###   Immigration, Public Housing and Support for the French National Front
###   Gloria Gennaro
###
###   Paper Figure A10
###
####################################################################################


library(tidyr)
library(miceadds)
library(AER)
library(rlist)
library(ggplot2)


################################################################################
# Set Up
################################################################################

# Load Working Directories
source(paste0(wd_main, '/2_code/00_working_directories.R'))


################################################################################
# Auxiliary Functions
################################################################################

makeplot = function(model_output, hete_var){
  
  groups = c(paste0('Low ', hete_var), paste0('Medium ', hete_var), paste0('High ', hete_var))
  
  container = lapply(model_output, function(x) rbind(cbind(x[2:8,], 
                                                           period = c(-5, -4, -3, -2, 0, 1, 2, 3)), c(0,0,NA,NA,-1)))
  
  container = list.rbind(container) %>% as.data.frame()
  container = cbind(container, group=rep(groups, each=8))
  colnames(container) = c('beta', 'se', 'tval', 'pval', 'period', 'group')
  container$group.f = factor(container$group, levels = groups)
  
  ggplot(data=container, aes(y=beta, x=factor(period), colour=group.f, shape = group.f))+
    geom_point(position = position_dodge(width = 0.5), size=3) + 
    geom_errorbar(aes(ymin=beta-1.96*se, ymax=beta+1.96*se), linewidth=1, width=.15, position = position_dodge(width = 0.5)) +
    geom_hline(yintercept=0, colour='red')+
    xlab("Period relative to crossing the population threshold") + 
    ylab("NF Vote Share (percentage points)") +
    theme_light()+
    theme(legend.position="bottom", legend.title = element_blank(), text = element_text(size=18))
}


placebos = function(bdw, outname){
  
  models = list()
  for (n in c(1,2,3)){
    
    print(paste0('Immigation tercile ', n, ' -------------- '))
    df1 = df_did[which(df_did$imm_quant_99==n),]
    
    print(nrow(df1))
    
    temp = lm(data=df1, fn ~  m5 + m4 + m3 + m2 + p0 + p1 + p2 + p3 + running + 
                factor(CODGEO) + factor(policy_period) )
    temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)
    
    models[[n]] = temp
  }
  
  # Plot
  makeplot(models, 'Immigration')
  ggsave(paste0(wd_res, '/figures/', outname, '.pdf'), height=7, width=10)
  
}


################################################################################
# Hyperparameters for estimation (Figure A10a)
################################################################################

# Chose here parameters for later. NB: this will affect the data loading step
bdw = 3500
rural = F
urban = F
excess = T

# Load data and clean
source(paste0(wd_code, '/03_data_load_clean_placebo.R'))

# Estimation
placebos(bdw, 'figA10a')




################################################################################
#  Estimation (Figure A10b)
################################################################################

# Hyperparameters
bdw = 1500
rural = T
urban = F
excess = F

# Load data and clean
source(paste0(wd_code, '/03_data_load_clean_placebo.R'))

# Estimation
placebos(bdw, 'figA10b')

